// @ts-ignore
import CryptoJS from 'crypto-js'
// 接口类型和方法
interface BaseType{
    sign():object;
}

class Signature implements BaseType{
    sign = () =>{
        let timestamp = Date.parse(new Date().toString())/1000
        let text = "_api_access_key=" + "ee77e1f1e8fd46948ce03d84a6f90f26" +
                                        "&_api_name=" + 'fsi-callService-prd' +
                                        "&_api_timestamp=" + timestamp + "&_api_version=1.0.0"
        let signature = stringify(CryptoJS.HmacSHA1(text,"8Jp8RlN/BgTW52aXqMZBn7eKf8c="))
        
        let header = {
        '_api_name': 'fsi-callService-prd',
        '_api_version': '1.0.0',
        '_api_timestamp': timestamp,
        '_api_access_key': "ee77e1f1e8fd46948ce03d84a6f90f26",
        '_api_signature': signature}

        return header
    }

}

//转base64
function stringify(wordArray: any) {
    // Shortcuts
    var words = wordArray.words;
    var sigBytes = wordArray.sigBytes;
    var map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';

    // Clamp excess bits
    wordArray.clamp();

    // Convert
    var base64Chars = [];
    for (var i = 0; i < sigBytes; i += 3) {
      var byte1 = (words[i >>> 2]       >>> (24 - (i % 4) * 8))       & 0xff;
      var byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;
      var byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;

      var triplet = (byte1 << 16) | (byte2 << 8) | byte3;

      for (var j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) {
        base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f));
      }
    }
    // Add padding
    var paddingChar = map.charAt(64);
    if (paddingChar) {
      while (base64Chars.length % 4) {
        base64Chars.push(paddingChar);
      }
    }
    return base64Chars.join('');
  }

const signature = new Signature()


export default signature;